import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  const history = ref([])
  const doubleCount = computed(() => count.value * 2)
  const isPositive = computed(() => count.value > 0)
  const isNegative = computed(() => count.value < 0)
  const historyCount = computed(() => history.value.length)
  function increment() {
    count.value++
    history.value.push(`增加: ${count.value}`)
  }

  function decrement() {
    count.value--
    history.value.push(`减少: ${count.value}`)
  }

  function incrementBy(amount) {
    count.value += amount
    history.value.push(`增加 ${amount}: ${count.value}`)
  }

  function reset() {
    count.value = 0
    history.value.push(`重置: 0`)
  }

  function clearHistory() {
    history.value = []
  }
  return {
    count,
    history,
    doubleCount,
    isPositive,
    isNegative,
    historyCount,
    increment,
    decrement,
    incrementBy,
    reset,
    clearHistory
  }
})
